//
//  MyCollectMusicViewController.m
//  海角音乐
//
//  Created by dllo on 15/8/20.
//  Copyright (c) 2015年 杨元石. All rights reserved.
//

#import "MyCollectMusicViewController.h"

@interface MyCollectMusicViewController ()
{
    UILabel *_singleLabel;
    UIImageView *_singleImage;
}
@end

@implementation MyCollectMusicViewController

- (void)dealloc
{
    [super dealloc];
}


- (void)viewWillAppear:(BOOL)animated
{
    _dataArr = [[DataBase shareDataBaseHandle] selectModel];
    
    if (_dataArr.count != 0) {
        
        [_mineView.tableView reloadData];
        NSLog(@"刷新页面");
        _mineView.tableView.separatorColor = [UIColor lightGrayColor];
        _singleImage.hidden = YES;
        
    }else {
        
        _singleImage = [[UIImageView alloc]initWithFrame:CGRectMake(0, 150, 200, 200)];
        _singleImage.center = CGPointMake(SCREEN_WIDTH/2, 350);
        [_singleImage setImage:[UIImage imageNamed:@"content-empty.png"]];
        _mineView.tableView.separatorColor = [UIColor clearColor];
        _singleImage.hidden = NO;
        [self.view addSubview:_singleImage];
        
    }

}
- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.view.backgroundColor = [UIColor whiteColor];
    
    self.TitleView = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 200*ADJUSTWIDTH, 30*ADJUSTWIDTH)];
    //self.TitleView.textColor = [UIColor whiteColor];
    self.TitleView.textAlignment = NSTextAlignmentCenter;
    self.TitleView.text = @"我的收藏";
    self.navigationItem.titleView = self.TitleView;
    
    [self setAutomaticallyAdjustsScrollViewInsets:NO];
    
    _dataArr = [[NSMutableArray alloc] init];
    
    _dataArr = [[DataBase shareDataBaseHandle] selectModel];
    
    _mineView = [[NormalView alloc] initWithFrame:CGRectMake(0, 64, SCREEN_WIDTH, SCREEN_HEIGHT)];
    _mineView.tableView.dataSource =self;
    _mineView.tableView.delegate = self;
    [_mineView getLocalImage:@"7.jpg"];
    [self.view addSubview:_mineView];
    [_mineView release];
    

    
    
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"编辑" style:UIBarButtonItemStyleDone target:self action:@selector(done)];
    
}



- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    ReDetailModel *reDetailmodel = [_dataArr objectAtIndex:indexPath.row];
    BarPlayer.dataArr = _dataArr;
    BarPlayer.index = indexPath.row;
    [self playeMusicWithModel:reDetailmodel.url];    
    
}

// 播放方法
- (void)playeMusicWithModel:(NSString *)url_Arr
{
    
    STKAudioPlayer *Legacy = [SinglePlayer shareHandleSinglePlayer];
    [Legacy play:url_Arr];
}


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return _dataArr.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    static NSString *cellId = @"cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellId];
    if (cell == NO) {
        
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:cellId];
    }
    if (_dataArr.count > indexPath.row) {
        ReDetailModel *model = [_dataArr objectAtIndex:indexPath.row];
        
        cell.textLabel.text = model.song_name;
        cell.detailTextLabel.text = model.singer_name;
    }
    
   
    
    return cell;
}



- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    return SCREEN_WIDTH*0.213333;
    
}

// 设置 哪一行的编辑按钮 状态 指定编辑样式
- (UITableViewCellEditingStyle) tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return _editingStyle;
}




- (void)setEditing:(BOOL)editing animated:(BOOL)animated
{
    [super setEditing:editing animated:animated];
    
    [_mineView.tableView setEditing:YES animated:YES];
}

//
- (void)done
{
    
    _isSele = !_isSele;
    
    if (_isSele) {
        [self.navigationItem.rightBarButtonItem setTitle:@"完成"];
        _editingStyle = UITableViewCellEditingStyleDelete;
        
        
    } else {
        
        _editingStyle = UITableViewCellEditingStyleNone;
        [self.navigationItem.rightBarButtonItem setTitle:@"编辑"];
    }
    
    NSLog(@"1233");
    
}


- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
    return YES;
}

- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
    // 删除的操作
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        
        ReDetailModel *mo = [_dataArr objectAtIndex:indexPath.row];
        
        [[DataBase shareDataBaseHandle] deleteModelInfo:mo.number];
        
        [_dataArr removeObjectAtIndex:indexPath.row];
        
        
        if (_dataArr.count == 0) {
            
            UIImageView *BBImage = [[UIImageView alloc]initWithFrame:CGRectMake(0, 150, 200, 200)];            
            BBImage.center = CGPointMake(SCREEN_WIDTH/2, 350);
            
            [BBImage setImage:[UIImage imageNamed:@"content-empty.png"]];
            _mineView.tableView.separatorColor = [UIColor clearColor];
            BBImage.hidden = NO;
            [self.view addSubview:BBImage];
            [_mineView.tableView reloadData];
            [BBImage release];
        }
        
        [_mineView.tableView reloadData];
    }
}





- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
